/*
 * Copyright (c) 2002-2004, xRhino, Inc.
 *
 * This library is free software; you can redistribute and/or modify
 * it under the terms of the dual license distributed with this source
 * package (a combined Trolltech QPL/GPL-compatible license)
 *
 * Under this dual license scheme, a separate license is available for use of 
 * this code with a proprietary (closed) system. For more information on the 
 * license, please see the LICENSE file distributed with this source package.
 */
#include "SugElement.h"

const char* SugElement::getStringAttribute(char* attrName)
{
   map<string, string>::iterator i;

   i = mAttributes.find(string(attrName));
   if (i != mAttributes.end())
      return (*i).second.c_str();
   return NULL;
}

bool SugElement::getIntegerAttribute(char* attrName, int& attrValue)
{
   const char* attrStr;
   
   attrStr = getStringAttribute(attrName);

   if (attrStr)
      attrValue = atoi(attrStr);
   return attrStr;
}

bool SugElement::getFloatAttribute(char* attrName, float& attrValue)
{
   const char* attrStr;
   
   attrStr = getStringAttribute(attrName);

   if (attrStr)
      attrValue = atof(attrStr);
   return attrStr;
}

bool SugElement::getColorAttribute(char* attrName, SugColor& attrValue)
{
   const char* attrStr;
   
   attrStr = getStringAttribute(attrName);

   if (attrStr)
      attrValue.setColor(attrStr);
   return attrStr;
}

bool SugElement::getPointListAttribute(char* attrName,
                                       vector<SugPoint>& attrValue)
{
   char* s;
   char* tok;
   char* sStart;
   const char* attrStr;
   SugPoint newPoint;
   
   attrStr = getStringAttribute(attrName);
   if (attrStr)
   {
      s = new char[strlen(attrStr) + 1];
      sStart = s;
      strcpy(s, attrStr);

      tok = strtok_r(s, " ,", &s);
      while (tok)
      {
         newPoint.setX(atof(tok));
         tok = strtok_r(NULL, " ,", &s);

         newPoint.setY(atof(tok));
         tok = strtok_r(NULL, " ,", &s);

         attrValue.push_back(newPoint);
      }

      delete sStart;
   }

   return attrStr;
}


bool SugElement::getTransformListAttribute(vector<SugTransform>& attrValue)
{
   char* s;
   char* tok;
   char* sStart;
   const char* attrStr;
   SugTransform newTransform;
   
   attrStr = getStringAttribute("transform");
   if (attrStr)
   {
      s = new char[strlen(attrStr) + 1];
      sStart = s;
      strcpy(s, attrStr);
      
      while (*s)
      {
         while (*s == ',' || *s == ' ')
            s++;
         tok = s;

         while (*s != ')')
            s++;
         s++;

         if (*s)
         {
            *s = '\0';
            s++;
         }
         
         newTransform.setTransform(tok);
         attrValue.push_back(newTransform);    
      }

      delete sStart;
   }
   return attrStr;
}

const char* SugElement::getId()
{
   return getStringAttribute("id");
}

SugTypes::ElementType SugElement::getElementType()
{
   return mElementType;
}

void SugElement::setElementType(SugTypes::ElementType newType)
{
   mElementType = newType;
}

void SugElement::addAttribute(char* name, char* value)
{
   mAttributes[string(name)] = string(value);
}

void SugElement::printAttributes()
{
   map<string, string>::iterator i;

   i = mAttributes.begin();
   while (i != mAttributes.end())
   {
      printf("attrib[%s] = %s\n", (*i).first.c_str(), (*i).second.c_str());
      i++;
   }
}

SugElement::SugElement()
{
}

SugElement::~SugElement()
{
}
